import {Component, OnInit} from '@angular/core';
import {CooperationService} from '../cooperation.service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {NzImageService} from 'ng-zorro-antd/image';

@Component({
  selector: 'app-cooperation-company',
  templateUrl: './cooperation-company.component.html',
  styleUrls: ['./cooperation-company.component.scss']
})
export class CooperationCompanyComponent implements OnInit {
  query = {
    name: '',
    // 当前页
    current: 1,
    // 分页大小
    size: 20,

  };
  listOfData = {
    current: 1,
    total: 100,
    size: 20,
    records: []
  };
  modal = {
    modalVisible: false,
    operationType: '',
    loading: false,
  };
  formData = {
    id: '',
    comName: '',
    comLogo: '',
    comAddress: '',
    comContant: '',
    comMail: '',
    comDesc: '',
    comPhone: '',
    contant: '',
    orgCode: '',
    identityCode: '',
  };

  constructor(
    private cooperationService: CooperationService,
    private nzImageService: NzImageService,
    private message: NzMessageService,
    private nzMessageService: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.queryList();
  }

  queryBtn() {
    this.query.current = 1;
    this.queryList();
  }

  resetQuery() {
    this.query = {
      name: '',
      current: 1,
      size: 20,

    };
    this.queryList();
  }

  // 查询列表
  queryList(): void {
    this.cooperationService.getCompany(this.query.current, this.query.size, this.query.name).then((res: any) => {
      if (res.code === 0) {
        this.listOfData = res.data;
      }
    });
  }

  onPageIndexChange(index: number) {
    this.query.current = index;
    this.queryList();
  }

  onPageSizeChange(size: number) {
    this.query.size = size;
    this.queryList();
  }

  // 查看
  view(data: any) {
    this.modal.modalVisible = true;
    this.modal.operationType = '查看';
    this.formData = data;
  }

  // 添加
  add() {
    this.modal.modalVisible = true;
    this.modal.operationType = '添加';
  }

  // 编辑
  edit(data: any) {
    this.modal.modalVisible = true;
    this.modal.operationType = '编辑';
    this.formData = JSON.parse(JSON.stringify(data));
  }

  // 模态框取消按钮
  handleModalCancel() {
    this.resetFormData();
  }

  // 模态框确认按钮监听
  handleModalOk() {
    if (!this.formData.comName) {
      this.nzMessageService.warning('请输入企业名称');
      return;
    }
    this.modal.loading = true;
    this.cooperationService.addOrModifyCompany(this.formData, this.modal.operationType === '添加').then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.queryList();
      }
    }).finally(() => {
      this.resetFormData();
    });
  }

  // 重置数据
  resetFormData() {
    this.formData = {
      id: '',
      comName: '',
      comLogo: '',
      comAddress: '',
      comContant: '',
      comMail: '',
      comDesc: '',
      comPhone: '',
      contant: '',
      orgCode: '',
      identityCode: '',
    };
    this.modal = {
      modalVisible: false,
      operationType: '',
      loading: false,
    };
  }

  // 删除分类
  deleteRow(id: any) {
    this.cooperationService.deleteCompany(id).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('删除成功');
        this.queryList();
      } else {
        this.nzMessageService.error('删除失败');
      }
    });
  }

  // 图片改变
  fileChange(event: any, form: any) {
    this.uploadImg(event.target.files[0]).then(url => {
      form.comLogo = url;
    }).catch(err => {
      this.message.error(err);
    }).finally(() => {
      event.target.files = null;
    });
  }

  // 上传图片
  uploadImg(img: any) {
    const file = new FormData();
    file.append('file', img);
    file.append('name', (+new Date()).toString());
    return new Promise((resolve, reject) => {
      this.cooperationService.httpPost(this.cooperationService.serviceUrl.fileUpload, file).then((res: any) => {
        if (res.code === 0) {
          resolve(res.data);
        } else {
          resolve(res.message);
        }
      }).catch(() => {
        reject('图片上传异常');
      });
    });
  }

  // 查看图片
  previewImage(src: string) {
    this.nzImageService.preview([{src, width: '400px', height: '400px'}], {nzZoom: 1.5, nzRotate: 0});
  }

  // 删除图片
  removeOptionImage(form: any) {
    form.comLogo = '';
  }
}
